/* add user code begin Header */
/**
  **************************************************************************
  * @file     wk_op.c
  * @brief    work bench config program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
/* add user code end Header */

/* Includes ------------------------------------------------------------------*/
#include "wk_op.h"
#include "wk_system.h"

/* add user code begin 0 */

/* add user code end 0 */

/**
  * @brief  init op function.
  * @param  none
  * @retval none
  */
void wk_op2_init(void)
{
  /* add user code begin op2_init 0 */

  /* add user code end op2_init 0 */

  gpio_init_type gpio_init_struct;
  uint32_t index = 0, cal_flag = 0;

  /* add user code begin op2_init 1 */

  /* add user code end op2_init 1 */

  gpio_default_para_init(&gpio_init_struct);

  /* configure the INP0 pin */
  gpio_init_struct.gpio_pins = GPIO_PINS_7;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOA, &gpio_init_struct);

  /* configure the INM0 pin */
  gpio_init_struct.gpio_pins = GPIO_PINS_5;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOA, &gpio_init_struct);

  /* configure the OUT pin */
  gpio_init_struct.gpio_pins = I_U_PIN;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(I_U_GPIO_PORT, &gpio_init_struct);

  /* initial op2 in calibration mode */
  op_set_mode(OP2, OP_CALIBRATION_MODE);
  op_set_trimming_mode(OP2, OP_TRIMMING_USER);
  op_calibration_select(OP2, OP_TRIMMING_PMOS);
  op_enable(OP2, TRUE);

  /* must delay 1ms for calibration */
  wk_delay_ms(1);
  /* calibration pmos */
  for(index=0; index<=0x1f; index++)
  {
    op_set_triming_value(OP2, OP_TRIMMING_PMOS, index);
    /* must delay 1ms for calibration */
    wk_delay_ms(1);
    /* calibration is successful when calout switches from 1 to 0 */		
    if(op_calout_status_get(OP2)==RESET)
    {
      cal_flag=1;
    }
    
    if(cal_flag==1)
      break;
  }
  cal_flag = 0;
  op_calibration_select(OP2, OP_TRIMMING_NMOS);
  /* must delay 1ms for calibration */
  wk_delay_ms(1);
  /* calibration nmos */
  for(index=0; index<=0x1f; index++)
  {
    op_set_triming_value(OP2, OP_TRIMMING_NMOS, index);
    /* must delay 1ms for calibration */
    wk_delay_ms(1);
    /* calibration  is successful when calout switches from 1 to 0 */		
    if(op_calout_status_get(OP2)==RESET)
    {
      cal_flag=1;
    }

    if(cal_flag==1)
      break;
  }

  /* initial op*/
  op_set_mode(OP2, OP_FUNCTIONAL_MODE);
  op_set_functional_mode(OP2, OP_STANDALONE_MODE);
  op_internal_output_select(OP2, OP_INTERNAL_OUTPUT_GPIO);

  op_set_highspeed_mode_enable(OP2, FALSE);

  /* default selection of the non inverting input */
  op_set_input_non_inverting(OP2, OP_NON_INVERTING_INPUT_INP0);

  /* default selection of the inverting input */
  op_set_input_inverting(OP2, OP_INVERTING_INPUT_INM0);

  /* enable op */
  op_enable(OP2, TRUE);

  /* add user code begin op2_init 2 */

  /* add user code end op2_init 2 */
}

/**
  * @brief  init op function.
  * @param  none
  * @retval none
  */
void wk_op3_init(void)
{
  /* add user code begin op3_init 0 */

  /* add user code end op3_init 0 */

  gpio_init_type gpio_init_struct;
  uint32_t index = 0, cal_flag = 0;

  /* add user code begin op3_init 1 */

  /* add user code end op3_init 1 */

  gpio_default_para_init(&gpio_init_struct);

  /* configure the INP0 pin */
  gpio_init_struct.gpio_pins = GPIO_PINS_0;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOB, &gpio_init_struct);

  /* configure the INM0 pin */
  gpio_init_struct.gpio_pins = GPIO_PINS_2;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOB, &gpio_init_struct);

  /* configure the OUT pin */
  gpio_init_struct.gpio_pins = GPIO_PINS_1;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOB, &gpio_init_struct);

  /* initial op3 in calibration mode */
  op_set_mode(OP3, OP_CALIBRATION_MODE);
  op_set_trimming_mode(OP3, OP_TRIMMING_USER);
  op_calibration_select(OP3, OP_TRIMMING_PMOS);
  op_enable(OP3, TRUE);

  /* must delay 1ms for calibration */
  wk_delay_ms(1);
  /* calibration pmos */
  for(index=0; index<=0x1f; index++)
  {
    op_set_triming_value(OP3, OP_TRIMMING_PMOS, index);
    /* must delay 1ms for calibration */
    wk_delay_ms(1);
    /* calibration is successful when calout switches from 1 to 0 */		
    if(op_calout_status_get(OP3)==RESET)
    {
      cal_flag=1;
    }
    
    if(cal_flag==1)
      break;
  }
  cal_flag = 0;
  op_calibration_select(OP3, OP_TRIMMING_NMOS);
  /* must delay 1ms for calibration */
  wk_delay_ms(1);
  /* calibration nmos */
  for(index=0; index<=0x1f; index++)
  {
    op_set_triming_value(OP3, OP_TRIMMING_NMOS, index);
    /* must delay 1ms for calibration */
    wk_delay_ms(1);
    /* calibration  is successful when calout switches from 1 to 0 */		
    if(op_calout_status_get(OP3)==RESET)
    {
      cal_flag=1;
    }

    if(cal_flag==1)
      break;
  }

  /* initial op*/
  op_set_mode(OP3, OP_FUNCTIONAL_MODE);
  op_set_functional_mode(OP3, OP_STANDALONE_MODE);
  op_internal_output_select(OP3, OP_INTERNAL_OUTPUT_GPIO);

  op_set_highspeed_mode_enable(OP3, FALSE);

  /* default selection of the non inverting input */
  op_set_input_non_inverting(OP3, OP_NON_INVERTING_INPUT_INP0);

  /* default selection of the inverting input */
  op_set_input_inverting(OP3, OP_INVERTING_INPUT_INM0);

  /* enable op */
  op_enable(OP3, TRUE);

  /* add user code begin op3_init 2 */

  /* add user code end op3_init 2 */
}

/**
  * @brief  init op function.
  * @param  none
  * @retval none
  */
void wk_op4_init(void)
{
  /* add user code begin op4_init 0 */

  /* add user code end op4_init 0 */

  gpio_init_type gpio_init_struct;
  uint32_t index = 0, cal_flag = 0;

  /* add user code begin op4_init 1 */

  /* add user code end op4_init 1 */

  gpio_default_para_init(&gpio_init_struct);

  /* configure the INP2 pin */
  gpio_init_struct.gpio_pins = GPIO_PINS_11;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOB, &gpio_init_struct);

  /* configure the INM0 pin */
  gpio_init_struct.gpio_pins = GPIO_PINS_10;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOB, &gpio_init_struct);

  /* configure the OUT pin */
  gpio_init_struct.gpio_pins = I_V_PIN;
  gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(I_V_GPIO_PORT, &gpio_init_struct);

  /* initial op4 in calibration mode */
  op_set_mode(OP4, OP_CALIBRATION_MODE);
  op_set_trimming_mode(OP4, OP_TRIMMING_USER);
  op_calibration_select(OP4, OP_TRIMMING_PMOS);
  op_enable(OP4, TRUE);

  /* must delay 1ms for calibration */
  wk_delay_ms(1);
  /* calibration pmos */
  for(index=0; index<=0x1f; index++)
  {
    op_set_triming_value(OP4, OP_TRIMMING_PMOS, index);
    /* must delay 1ms for calibration */
    wk_delay_ms(1);
    /* calibration is successful when calout switches from 1 to 0 */		
    if(op_calout_status_get(OP4)==RESET)
    {
      cal_flag=1;
    }
    
    if(cal_flag==1)
      break;
  }
  cal_flag = 0;
  op_calibration_select(OP4, OP_TRIMMING_NMOS);
  /* must delay 1ms for calibration */
  wk_delay_ms(1);
  /* calibration nmos */
  for(index=0; index<=0x1f; index++)
  {
    op_set_triming_value(OP4, OP_TRIMMING_NMOS, index);
    /* must delay 1ms for calibration */
    wk_delay_ms(1);
    /* calibration  is successful when calout switches from 1 to 0 */		
    if(op_calout_status_get(OP4)==RESET)
    {
      cal_flag=1;
    }

    if(cal_flag==1)
      break;
  }

  /* initial op*/
  op_set_mode(OP4, OP_FUNCTIONAL_MODE);
  op_set_functional_mode(OP4, OP_STANDALONE_MODE);
  op_internal_output_select(OP4, OP_INTERNAL_OUTPUT_GPIO);

  op_set_highspeed_mode_enable(OP4, FALSE);

  /* default selection of the non inverting input */
  op_set_input_non_inverting(OP4, OP_NON_INVERTING_INPUT_INP2);

  /* default selection of the inverting input */
  op_set_input_inverting(OP4, OP_INVERTING_INPUT_INM0);

  /* enable op */
  op_enable(OP4, TRUE);

  /* add user code begin op4_init 2 */

  /* add user code end op4_init 2 */
}

/* add user code begin 1 */

/* add user code end 1 */
